home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Processes / MP Threaded Sort / Sort / SortPicts3.cp < prev    next >
Encoding:
Text File  |  1997-03-13  |  1.8 KB  |  87 lines  |  [TEXT/CWIE]

  1. #include "SortPicts.h"
  2.  
  3.  
  4.  
  5. void    SortPicts::UseSortData( void)
  6. {
  7.     sortPixmap = GetGWorldPixMap( sortGWorld);
  8.  
  9.     LockPixels( sortPixmap);
  10.  
  11.     sortPixels = (SortPixelPtr) GetPixBaseAddr( sortPixmap);
  12.     
  13.     if ( !coplandTask )
  14.     {
  15.         HLock( (Handle) sortHandle);
  16.         
  17.         sortData = *sortHandle;
  18.     }
  19. }
  20.  
  21. void    SortPicts::UnuseSortData( void)
  22. {
  23.     if ( !coplandTask ) {
  24.         HUnlock( (Handle) sortHandle);
  25.     
  26.         UnlockPixels( sortPixmap);
  27.     }
  28. }
  29.  
  30.  
  31.  
  32.  
  33. /*
  34.  *    RandomPixel
  35.  *  -----------
  36.  *
  37.  *
  38.  *    RandomPixel will return a random number, x, between 0 ≤ x < N
  39.  *    where N is the number of pixels in the image.
  40.  */
  41. long    SortPicts::RandomPixel( void)        //    Return a # between 0 <= x < max
  42. {
  43.     return Random( N);
  44. }
  45.  
  46. // need our oun random routine since we can't call the toolbox random routine from an MP task.
  47.  
  48. /*
  49.  *    Random
  50.  *    ------
  51.  *    Usage:        r = Random( num)
  52.  *
  53.  *    Random will return a random number, r, 0 ≤ r < num
  54.  */
  55. long    SortPicts::Random( long num)
  56. {
  57.     long            randomNum = 0x7FFFFFFF;        //    The ultimate long
  58.     long            tempN;
  59.     short            numNBits;
  60.     short            numShiftBits, numRandomBits;
  61.     
  62. //    Random Number Initialization
  63.     tempN = num;
  64.     numNBits = 1;                // Using the additive conguential method (Random numbers)
  65.     while (tempN >>= 1)            // Count the number of bits required to hold N    
  66.         ++numNBits;                //    example:  to hold 0x37 requires 6 bits (0-3F)
  67.  
  68.     numRandomBits = 31;    
  69.     numShiftBits = numRandomBits - numNBits;
  70.     
  71.     if( randomSeed < 0)
  72.         randomSeed = -randomSeed;
  73.     
  74.     while( randomNum >= num || randomNum < 0)
  75.     {
  76. //        RandomNum generator
  77.         randomNum = randomSeed = (randomSeed & 1) ^ ((randomSeed & (1L << 28)) >> 28)
  78.                                ? (randomSeed >> 1) : (randomSeed >> 1) | 0x40000000;
  79. //        This next line "mixes" some of the upper bits with the lower bits
  80.         randomNum = randomNum & (0xFFFFFFFF >> numShiftBits);
  81. //        randomNum = (randomNum >> numShiftBits) ^ (randomNum & (0x7FFFFFFF >> numShiftBits));
  82.     }
  83.     
  84.     return randomNum;    
  85. }
  86.  
  87.